home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / gas / atof-tar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-17  |  15.1 KB  |  585 lines

  1. /* atof_ieee.c - turn a Flonum into an IEEE floating point number
  2.    Copyright (C) 1987, 1992 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GAS; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "as.h"
  21.  
  22. extern FLONUM_TYPE generic_floating_point_number;    /* Flonums returned here. */
  23.  
  24. #ifndef NULL
  25. #define NULL (0)
  26. #endif
  27.  
  28. extern const char EXP_CHARS[];
  29. /* Precision in LittleNums. */
  30. #define MAX_PRECISION (6)
  31. #define F_PRECISION (2)
  32. #define D_PRECISION (4)
  33. #define X_PRECISION (6)
  34. #define P_PRECISION (6)
  35.  
  36. /* Length in LittleNums of guard bits. */
  37. #define GUARD (2)
  38.  
  39. static const unsigned long mask[] =
  40. {
  41.   0x00000000,
  42.   0x00000001,
  43.   0x00000003,
  44.   0x00000007,
  45.   0x0000000f,
  46.   0x0000001f,
  47.   0x0000003f,
  48.   0x0000007f,
  49.   0x000000ff,
  50.   0x000001ff,
  51.   0x000003ff,
  52.   0x000007ff,
  53.   0x00000fff,
  54.   0x00001fff,
  55.   0x00003fff,
  56.   0x00007fff,
  57.   0x0000ffff,
  58.   0x0001ffff,
  59.   0x0003ffff,
  60.   0x0007ffff,
  61.   0x000fffff,
  62.   0x001fffff,
  63.   0x003fffff,
  64.   0x007fffff,
  65.   0x00ffffff,
  66.   0x01ffffff,
  67.   0x03ffffff,
  68.   0x07ffffff,
  69.   0x0fffffff,
  70.   0x1fffffff,
  71.   0x3fffffff,
  72.   0x7fffffff,
  73.   0xffffffff,
  74. };
  75.  
  76.  
  77. static int bits_left_in_littlenum;
  78. static int littlenums_left;
  79. static LITTLENUM_TYPE *littlenum_pointer;
  80.  
  81. static int
  82. next_bits (number_of_bits)
  83.      int number_of_bits;
  84. {
  85.   int return_value;
  86.  
  87.   if (!littlenums_left)
  88.     return (0);
  89.   if (number_of_bits >= bits_left_in_littlenum)
  90.     {
  91.       return_value = mask[bits_left_in_littlenum] & *littlenum_pointer;
  92.       number_of_bits -= bits_left_in_littlenum;
  93.       return_value <<= number_of_bits;
  94.  
  95.       if (--littlenums_left)
  96.     {
  97.       bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
  98.       --littlenum_pointer;
  99.       return_value |= (*littlenum_pointer >> bits_left_in_littlenum) & mask[number_of_bits];
  100.     }
  101.     }
  102.   else
  103.     {
  104.       bits_left_in_littlenum -= number_of_bits;
  105.       return_value = mask[number_of_bits] & (*littlenum_pointer >> bits_left_in_littlenum);
  106.     }
  107.   return (return_value);
  108. }
  109.  
  110. /* Num had better be less than LITTLENUM_NUMBER_OF_BITS */
  111. static void
  112. unget_bits (num)
  113.      int num;
  114. {
  115.   if (!littlenums_left)
  116.     {
  117.       ++littlenum_pointer;
  118.       ++littlenums_left;
  119.       bits_left_in_littlenum = num;
  120.     }
  121.   else if (bits_left_in_littlenum + num > LITTLENUM_NUMBER_OF_BITS)
  122.     {
  123.       bits_left_in_littlenum = num - (LITTLENUM_NUMBER_OF_BITS - bits_left_in_littlenum);
  124.       ++littlenum_pointer;
  125.       ++littlenums_left;
  126.     }
  127.   else
  128.     bits_left_in_littlenum += num;
  129. }
  130.  
  131. static void
  132. make_invalid_floating_point_number (words)
  133.      LITTLENUM_TYPE *words;
  134. {
  135.   as_bad ("cannot create floating-point number");
  136.   words[0] = (LITTLENUM_TYPE) ((unsigned) -1) >> 1; /* Zero the leftmost bit */
  137.   words[1] = (LITTLENUM_TYPE) -1;
  138.   words[2] = (LITTLENUM_TYPE) -1;
  139.   words[3] = (LITTLENUM_TYPE) -1;
  140.   words[4] = (LITTLENUM_TYPE) -1;
  141.   words[5] = (LITTLENUM_TYPE) -1;
  142. }
  143.  
  144. /************************************************************************\
  145.  *    Warning: this returns 16-bit LITTLENUMs. It is up to the caller    *
  146.  *    to figure out any alignment problems and to conspire for the    *
  147.  *    bytes/word to be emitted in the right order. Bigendians beware!    *
  148.  *                                    *
  149. \************************************************************************/
  150.  
  151. /* Note that atof-ieee always has X and P precisions enabled.  it is up
  152.    to md_atof to filter them out if the target machine does not support
  153.    them.  */
  154.  
  155. /* Returns pointer past text consumed. */
  156. char *
  157. atof_ieee (str, what_kind, words)
  158.      char *str;            /* Text to convert to binary. */
  159.      char what_kind;        /* 'd', 'f', 'g', 'h' */
  160.      LITTLENUM_TYPE *words;    /* Build the binary here. */
  161. {
  162.   /* Extra bits for zeroed low-order bits.  The 1st MAX_PRECISION are
  163.      zeroed, the last contain flonum bits. */
  164.   static LITTLENUM_TYPE bits[MAX_PRECISION + MAX_PRECISION + GUARD];
  165.   char *return_value;
  166.   /* Number of 16-bit words in the format. */
  167.   int precision;
  168.   long exponent_bits;
  169.   FLONUM_TYPE save_gen_flonum;
  170.  
  171.   /* We have to save the generic_floating_point_number because it
  172.      contains storage allocation about the array of LITTLENUMs where
  173.      the value is actually stored.  We will allocate our own array of
  174.      littlenums below, but have to restore the global one on exit.  */
  175.   save_gen_flonum = generic_floating_point_number;
  176.  
  177.   return_value = str;
  178.   generic_floating_point_number.low = bits + MAX_PRECISION;
  179.   generic_floating_point_number.high = NULL;
  180.   generic_floating_point_number.leader = NULL;
  181.   generic_floating_point_number.exponent = 0;
  182.   generic_floating_point_number.sign = '\0';
  183.  
  184.   /* Use more LittleNums than seems necessary: the highest flonum may
  185.      have 15 leading 0 bits, so could be useless. */
  186.  
  187.   memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION);
  188.  
  189.   switch (what_kind)
  190.     {
  191.     case 'f':
  192.     case 'F':
  193.     case 's':
  194.     case 'S':
  195.       precision = F_PRECISION;
  196.       exponent_bits = 8;
  197.       break;
  198.  
  199.     case 'd':
  200.     case 'D':
  201.     case 'r':
  202.     case 'R':
  203.       precision = D_PRECISION;
  204.       exponent_bits = 11;
  205.       break;
  206.  
  207.     case 'x':
  208.     case 'X':
  209.     case 'e':
  210.     case 'E':
  211.       precision = X_PRECISION;
  212.       exponent_bits = 15;
  213.       break;
  214.  
  215.     case 'p':
  216.     case 'P':
  217.  
  218.       precision = P_PRECISION;
  219.       exponent_bits = -1;
  220.       break;
  221.  
  222.     default:
  223.       make_invalid_floating_point_number (words);
  224.       return (NULL);
  225.     }
  226.  
  227.   generic_floating_point_number.high
  228.     = generic_floating_point_number.low + precision - 1 + GUARD;
  229.  
  230.   if (atof_generic (&return_value, ".", EXP_CHARS,
  231.             &generic_floating_point_number))
  232.     {
  233.       make_invalid_floating_point_number (words);
  234.       return (NULL);
  235.     }
  236.   gen_to_words (words, precision, exponent_bits);
  237.  
  238.   /* Restore the generic_floating_point_number's storage alloc (and
  239.      everything else).  */
  240.   generic_floating_point_number = save_gen_flonum;
  241.  
  242.   return return_value;
  243. }
  244.  
  245. /* Turn generic_floating_point_number into a real float/double/extended.  */
  246. int
  247. gen_to_words (words, precision, exponent_bits)
  248.      LITTLENUM_TYPE *words;
  249.      int precision;
  250.      long exponent_bits;
  251. {
  252.   int return_value = 0;
  253.  
  254.   long exponent_1;
  255.   long exponent_2;
  256.   long exponent_3;
  257.   long exponent_4;
  258.   int exponent_skippage;
  259.   LITTLENUM_TYPE word1;
  260.   LITTLENUM_TYPE *lp;
  261.  
  262.   if (generic_floating_point_number.low > generic_floating_point_number.leader)
  263.     {
  264.       /* 0.0e0 seen. */
  265.       if (generic_floating_point_number.sign == '+')
  266.     words[0] = 0x0000;
  267.       else
  268.     words[0] = 0x8000;
  269.       memset (&words[1], '\0', sizeof (LITTLENUM_TYPE) * (precision - 1));
  270.       return (return_value);
  271.     }
  272.  
  273.   /* NaN:  Do the right thing */
  274.   if (generic_floating_point_number.sign == 0)
  275.     {
  276.       if (precision == F_PRECISION)
  277.     {
  278.       words[0] = 0x7fff;
  279.       words[1] = 0xffff;
  280.     }
  281.       else
  282.     {
  283.       words[0] = 0x7fff;
  284.       words[1] = 0xffff;
  285.       words[2] = 0xffff;
  286.       words[3] = 0xffff;
  287.     }
  288.       return return_value;
  289.     }
  290.   else if (generic_floating_point_number.sign == 'P')
  291.     {
  292.       /* +INF:  Do the right thing */
  293.       if (precision == F_PRECISION)
  294.     {
  295.       words[0] = 0x7f80;
  296.       words[1] = 0;
  297.     }
  298.       else
  299.     {
  300.       words[0] = 0x7ff0;
  301.       words[1] = 0;
  302.       words[2] = 0;
  303.       words[3] = 0;
  304.     }
  305.       return (return_value);
  306.     }
  307.   else if (generic_floating_point_number.sign == 'N')
  308.     {
  309.       /* Negative INF */
  310.       if (precision == F_PRECISION)
  311.     {
  312.       words[0] = 0xff80;
  313.       words[1] = 0x0;
  314.     }
  315.       else
  316.     {
  317.       words[0] = 0xfff0;
  318.       words[1] = 0x0;
  319.       words[2] = 0x0;
  320.       words[3] = 0x0;
  321.     }
  322.       return (return_value);
  323.     }
  324.   /*
  325.    * The floating point formats we support have:
  326.    * Bit 15 is sign bit.
  327.    * Bits 14:n are excess-whatever exponent.
  328.    * Bits n-1:0 (if any) are most significant bits of fraction.
  329.    * Bits 15:0 of the next word(s) are the next most significant bits.
  330.    *
  331.    * So we need: number of bits of exponent, number of bits of
  332.    * mantissa.
  333.    */
  334.   bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
  335.   littlenum_pointer = generic_floating_point_number.leader;
  336.   littlenums_left = (1
  337.              + generic_floating_point_number.leader
  338.              - generic_floating_point_number.low);
  339.   /* Seek (and forget) 1st significant bit */
  340.   for (exponent_skippage = 0; !next_bits (1); ++exponent_skippage);;
  341.   exponent_1 = (generic_floating_point_number.exponent
  342.         + generic_floating_point_number.leader
  343.         + 1
  344.         - generic_floating_point_number.low);
  345.   /* Radix LITTLENUM_RADIX, point just higher than
  346.      generic_floating_point_number.leader. */
  347.   exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
  348.   /* Radix 2. */
  349.   exponent_3 = exponent_2 - exponent_skippage;
  350.   /* Forget leading zeros, forget 1st bit. */
  351.   exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2);
  352.   /* Offset exponent. */
  353.  
  354.   lp = words;
  355.  
  356.   /* Word 1. Sign, exponent and perhaps high bits. */
  357.   word1 = ((generic_floating_point_number.sign == '+')
  358.        ? 0
  359.        : (1 << (LITTLENUM_NUMBER_OF_BITS - 1)));
  360.  
  361.   /* Assume 2's complement integers. */
  362.   if (exponent_4 < 1 && exponent_4 >= -62)
  363.     {
  364.       int prec_bits;
  365.       int num_bits;
  366.  
  367.       unget_bits (1);
  368.       num_bits = -exponent_4;
  369.       prec_bits = LITTLENUM_NUMBER_OF_BITS * precision - (exponent_bits + 1 + num_bits);
  370.       if (precision == X_PRECISION && exponent_bits == 15)
  371.     prec_bits -= LITTLENUM_NUMBER_OF_BITS + 1;
  372.  
  373.       if (num_bits >= LITTLENUM_NUMBER_OF_BITS - exponent_bits)
  374.     {
  375.       /* Bigger than one littlenum */
  376.       num_bits -= (LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits;
  377.       *lp++ = word1;
  378.       if (num_bits + exponent_bits + 1 >= precision * LITTLENUM_NUMBER_OF_BITS)
  379.         {
  380.           /* Exponent overflow */
  381.           make_invalid_floating_point_number (words);
  382.           return (return_value);
  383.         }
  384.       if (precision == X_PRECISION && exponent_bits == 15)
  385.         {
  386.           *lp++ = 0;
  387.           *lp++ = 0;
  388.           num_bits -= LITTLENUM_NUMBER_OF_BITS - 1;
  389.         }
  390.       while (num_bits >= LITTLENUM_NUMBER_OF_BITS)
  391.         {
  392.           num_bits -= LITTLENUM_NUMBER_OF_BITS;
  393.           *lp++ = 0;
  394.         }
  395.       if (num_bits)
  396.         *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - (num_bits));
  397.     }
  398.       else
  399.     {
  400.       if (precision == X_PRECISION && exponent_bits == 15)
  401.         {
  402.           *lp++ = word1;
  403.           *lp++ = 0;
  404.           if (num_bits == LITTLENUM_NUMBER_OF_BITS)
  405.         {
  406.           *lp++ = 0;
  407.           *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - 1);
  408.         }
  409.           else if (num_bits == LITTLENUM_NUMBER_OF_BITS - 1)
  410.         *lp++ = 0;
  411.           else
  412.         *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - 1 - num_bits);
  413.           num_bits = 0;
  414.         }
  415.       else
  416.         {
  417.           word1 |= next_bits ((LITTLENUM_NUMBER_OF_BITS - 1) - (exponent_bits + num_bits));
  418.           *lp++ = word1;
  419.         }
  420.     }
  421.       while (lp < words + precision)
  422.     *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS);
  423.  
  424.       /* Round the mantissa up, but don't change the number */
  425.       if (next_bits (1))
  426.     {
  427.       --lp;
  428.       if (prec_bits > LITTLENUM_NUMBER_OF_BITS)
  429.         {
  430.           int n = 0;
  431.           int tmp_bits;
  432.  
  433.           n = 0;
  434.           tmp_bits = prec_bits;
  435.           while (tmp_bits > LITTLENUM_NUMBER_OF_BITS)
  436.         {
  437.           if (lp[n] != (LITTLENUM_TYPE) - 1)
  438.             break;
  439.           --n;
  440.           tmp_bits -= LITTLENUM_NUMBER_OF_BITS;
  441.         }
  442.           if (tmp_bits > LITTLENUM_NUMBER_OF_BITS || (lp[n] & mask[tmp_bits]) != mask[tmp_bits])
  443.         {
  444.           unsigned long carry;
  445.  
  446.           for (carry = 1; carry && (lp >= words); lp--)
  447.             {
  448.               carry = *lp + carry;
  449.               *lp = carry;
  450.               carry >>= LITTLENUM_NUMBER_OF_BITS;
  451.             }
  452.         }
  453.         }
  454.       else if ((*lp & mask[prec_bits]) != mask[prec_bits])
  455.         lp++;
  456.     }
  457.  
  458.       return return_value;
  459.     }
  460.   else if (exponent_4 & ~mask[exponent_bits])
  461.     {
  462.       /*
  463.        * Exponent overflow. Lose immediately.
  464.        */
  465.  
  466.       /*
  467.        * We leave return_value alone: admit we read the
  468.        * number, but return a floating exception
  469.        * because we can't encode the number.
  470.        */
  471.       make_invalid_floating_point_number (words);
  472.       return return_value;
  473.     }
  474.   else
  475.     {
  476.       word1 |= (exponent_4 << ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits))
  477.     | next_bits ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits);
  478.     }
  479.  
  480.   *lp++ = word1;
  481.  
  482.   /* X_PRECISION is special: it has 16 bits of zero in the middle,
  483.      followed by a 1 bit. */
  484.   if (exponent_bits == 15 && precision == X_PRECISION)
  485.     {
  486.       *lp++ = 0;
  487.       *lp++ = 1 << (LITTLENUM_NUMBER_OF_BITS) | next_bits (LITTLENUM_NUMBER_OF_BITS - 1);
  488.     }
  489.  
  490.   /* The rest of the words are just mantissa bits. */
  491.   while (lp < words + precision)
  492.     *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS);
  493.  
  494.   if (next_bits (1))
  495.     {
  496.       unsigned long carry;
  497.       /*
  498.        * Since the NEXT bit is a 1, round UP the mantissa.
  499.        * The cunning design of these hidden-1 floats permits
  500.        * us to let the mantissa overflow into the exponent, and
  501.        * it 'does the right thing'. However, we lose if the
  502.        * highest-order bit of the lowest-order word flips.
  503.        * Is that clear?
  504.        */
  505.  
  506.       /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
  507.      Please allow at least 1 more bit in carry than is in a LITTLENUM.
  508.      We need that extra bit to hold a carry during a LITTLENUM carry
  509.      propagation. Another extra bit (kept 0) will assure us that we
  510.      don't get a sticky sign bit after shifting right, and that
  511.      permits us to propagate the carry without any masking of bits.
  512.      #endif */
  513.       for (carry = 1, lp--; carry && (lp >= words); lp--)
  514.     {
  515.       carry = *lp + carry;
  516.       *lp = carry;
  517.       carry >>= LITTLENUM_NUMBER_OF_BITS;
  518.     }
  519.       if ((word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)))
  520.     {
  521.       /* We leave return_value alone: admit we read the
  522.        * number, but return a floating exception
  523.        * because we can't encode the number.
  524.        */
  525.       *words &= ~(1 << (LITTLENUM_NUMBER_OF_BITS - 1));
  526.       /* make_invalid_floating_point_number (words); */
  527.       /* return return_value; */
  528.     }
  529.     }
  530.   return (return_value);
  531. }
  532.  
  533. #if 0 /* unused */
  534. /* This routine is a real kludge.  Someone really should do it better,
  535.    but I'm too lazy, and I don't understand this stuff all too well
  536.    anyway. (JF)  */
  537. static void
  538. int_to_gen (x)
  539.      long x;
  540. {
  541.   char buf[20];
  542.   char *bufp;
  543.  
  544.   sprintf (buf, "%ld", x);
  545.   bufp = &buf[0];
  546.   if (atof_generic (&bufp, ".", EXP_CHARS, &generic_floating_point_number))
  547.     as_bad ("Error converting number to floating point (Exponent overflow?)");
  548. }
  549. #endif
  550.  
  551. #ifdef TEST
  552. char *
  553. print_gen (gen)
  554.      FLONUM_TYPE *gen;
  555. {
  556.   FLONUM_TYPE f;
  557.   LITTLENUM_TYPE arr[10];
  558.   double dv;
  559.   float fv;
  560.   static char sbuf[40];
  561.  
  562.   if (gen)
  563.     {
  564.       f = generic_floating_point_number;
  565.       generic_floating_point_number = *gen;
  566.     }
  567.   gen_to_words (&arr[0], 4, 11);
  568.   memcpy (&dv, &arr[0], sizeof (double));
  569.   sprintf (sbuf, "%x %x %x %x %.14G   ", arr[0], arr[1], arr[2], arr[3], dv);
  570.   gen_to_words (&arr[0], 2, 8);
  571.   memcpy (&fv, &arr[0], sizeof (float));
  572.   sprintf (sbuf + strlen (sbuf), "%x %x %.12g\n", arr[0], arr[1], fv);
  573.  
  574.   if (gen)
  575.     {
  576.       generic_floating_point_number = f;
  577.     }
  578.  
  579.   return (sbuf);
  580. }
  581.  
  582. #endif
  583.  
  584. /* end of atof-ieee.c */
  585.